package org.jeecg.modules.patient;

import cn.hutool.extra.pinyin.PinyinUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.inquiry.service.IInquiryInfoService;
import org.jeecg.common.modules.patient.entity.Patient;
import org.jeecg.common.modules.patient.service.IPatientService;
import org.jeecg.common.modules.patient.vo.PatientReqVo;
import org.jeecg.common.modules.wx.entity.WxUserinfo;
import org.jeecg.common.modules.wx.service.IWxUserinfoService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.data.service.PageReturn;
import org.jeecg.modules.system.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: patient
 * @Author: jeecg-boot
 * @Date:   2020-08-19
 * @Version: V1.0
 */
@Api(tags="patient")
@RestController
@RequestMapping("/patient/patient")
@Slf4j
public class PatientController extends JeecgController<Patient, IPatientService> {
	@Autowired
	private IPatientService patientService;

	@Autowired
	private IWxUserinfoService wxUserinfoService;

	@Autowired
	private IInquiryInfoService inquiryInfoService;

	/**
	 * 分页列表查询
	 *
	 * @param patient
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "patient-分页列表查询")
	@ApiOperation(value="patient-分页列表查询", notes="patient-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Patient patient,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		//数据开始的记录数
		int dataStart = (pageNo - 1) * pageSize;

		//先查询所有的患者、用户信息
		String name     = req.getParameter("name");
		String nickname = req.getParameter("nickname");
		String mobile   = req.getParameter("mobile");
		List<PatientReqVo> patientReqVos = patientService.getPatientInfos(name,nickname,mobile,dataStart,pageSize);

		//查询到的总数据数
		Integer count = patientService.getCount(name,nickname,mobile);
		//组合患者和问诊单数量数据
		List responseInfo = new ArrayList();          //定义新组合名称
		for (PatientReqVo one : patientReqVos){
			Integer num = inquiryInfoService.getInquiryNum(one.getId()); //获取患者的问诊单数量
			PatientResponseVo res = this.initPatientResponseVo(one,num);
			responseInfo.add(res);
		}
		//组合分页展示的数据
		PageReturn pageReturn = new PageReturn();
		ResultVo vo = pageReturn.initResultVo(responseInfo,count,pageNo,pageSize);

		return Result.ok(vo);
	}

	/**
	 * 组合患者和问诊单数量的数据
	 * @param patientReqVo
	 * @param num
	 * @return
	 */
	private PatientResponseVo initPatientResponseVo(PatientReqVo patientReqVo , Integer num){
		PatientResponseVo vo = new PatientResponseVo();
		vo.setId(patientReqVo.getId());
		vo.setPatientName(patientReqVo.getName());
		vo.setToUserName(patientReqVo.getNickname());
		vo.setInquiryNum(num);
		vo.setRelation(patientReqVo.getRelation());
		return vo;
	}

	/**
	 * 处理患者姓名的拼音和首字母
	 */
	@AutoLog(value = "patient")
	@ApiOperation(value="patient", notes="patient-处理患者姓名的拼音和首字母")
	@GetMapping(value = "/operateInfo")
	public Result<?> operatePatientInfo( @RequestParam(name="id") String id){
		Patient patient = patientService.getById(id);
		String pinyin = null;
		String first  = null;
		try {
			pinyin = PinyinUtil.getPinyin(patient.getName(),"");
			first = PinyinUtil.getFirstLetter(patient.getName(),"");
			if (first.length() > 0){
				first = first.substring(0,1);
			}
		} catch (Exception e) {
			log.error("获取患者拼音失败{}",patient.getName(),e);
		}
		return Result.ok("拼音："+pinyin+"；首字母："+first);
	}

	/**
	 *   添加
	 *
	 * @param patient
	 * @return
	 */
	@AutoLog(value = "patient-添加")
	@ApiOperation(value="patient-添加", notes="patient-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Patient patient) {
		patientService.save(patient);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param patient
	 * @return
	 */
	@AutoLog(value = "patient-编辑")
	@ApiOperation(value="patient-编辑", notes="patient-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Patient patient) {
		patientService.updateById(patient);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "patient-通过id删除")
	@ApiOperation(value="patient-通过id删除", notes="patient-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		patientService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "patient-批量删除")
	@ApiOperation(value="patient-批量删除", notes="patient-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.patientService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "patient-通过id查询")
	@ApiOperation(value="patient-通过id查询", notes="patient-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Patient patient = patientService.getById(id);
		if(patient==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(patient);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param patient
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, Patient patient) {
		return super.exportXls(request, patient, Patient.class, "patient");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, Patient.class);
	}

}
